package com.xywl.escort.view.pay.project;

import com.xywl.escort.bean.BaseResp;
import com.xywl.escort.bean.PayApplyResp;
import com.xywl.escort.bean.PayConfirmReq;
import com.xywl.escort.bean.VerifyPayPwdReq;
import com.xywl.escort.net.BaseSubscriber;
import com.xywl.escort.net.ExceptionHandle;
import com.xywl.escort.net.api.service.ApiServer;
import com.xywl.escort.utils.RxUtil;

import javax.inject.Inject;

/**
 * Created by 32422 on 2017/10/25.
 */

public class ProjectPayConfirmPresenter implements ProjectPayConfirmContract.Presenter {
    private ProjectPayConfirmContract.View mView;
    private ApiServer mApiServer;

    @Inject
    ProjectPayConfirmPresenter(ApiServer apiServer) {
        mApiServer = apiServer;
    }

    @Override
    public void subscribe() {

    }

    @Override
    public void unSubscribe() {
        mView = null;
    }

    @Override
    public void takeView(ProjectPayConfirmContract.View view) {
        this.mView = view;
    }

    @Override
    public void payApply(String id, String payPwd) {
        VerifyPayPwdReq req = new VerifyPayPwdReq();
        req.id = id;
        req.payPwd = payPwd;
        mApiServer.payApply(req)
                .compose(RxUtil.<PayApplyResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<PayApplyResp>bindToLife())
                .subscribe(new BaseSubscriber<PayApplyResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.payApplyFailure("支付密码错误，请重新输入");
                    }

                    @Override
                    public void onNext(PayApplyResp resp) {
                        if (null != resp) {
                            if (resp.isOk() && null != resp.data) {
                                mView.payApplySuccess(resp.data);
                            } else {
                                mView.payApplyFailure(resp.msg);
                            }
                        } else {
                            mView.payApplyFailure("支付密码错误，请重新输入");
                        }
                    }
                });
    }

    @Override
    public void payConfirm(PayConfirmReq req) {
        mApiServer.payConfirm(req)
                .compose(RxUtil.<BaseResp>applySchedulers(RxUtil.IO_ON_UI_TRANSFORMER_BACK_PRESSURE))
                .compose(mView.<BaseResp>bindToLife())
                .subscribe(new BaseSubscriber<BaseResp>() {
                    @Override
                    protected void hideDialog() {

                    }

                    @Override
                    protected void showDialog() {

                    }

                    @Override
                    public void onError(ExceptionHandle.ResponseThrowable e) {
                        mView.payConfirmFailure("支付失败");
                    }

                    @Override
                    public void onNext(BaseResp resp) {
                        if (null != resp) {
                            if (resp.isOk()) {
                                mView.payConfirmSuccess(resp);
                            } else {
                                mView.payConfirmFailure(resp.msg);
                            }
                        } else {
                            mView.payConfirmFailure("支付失败");
                        }
                    }
                });
    }
}
